using System;
using System.IO;
using System.Text;
	
namespace linenumbers
{
	class LineNumbers
	{	
		public static void Main(string[] args)
		{
			// make sure the rigth amount of parameters is given
			if(args.Length < 1 || args.Length > 2) {
				Console.WriteLine("usage: Main.cs <Source file> (-n)");
				Environment.Exit(1);
			} else if(args.Length == 1) {
				LineCount lCount = new LineCount(args[0]);
				int max = MaxPadCount(lCount.GetLines());
				
				//check if the file exist
				/*try {
					File.Open(args[0], FileMode.Open);
				}
				catch(FileNotFoundException e) {
					Console.WriteLine("Exception caught: {0}", e);
					Environment.Exit(1);
				} */
				
				//creating the streams to read and write from given file
				FileStream fStream = new FileStream(args[0], FileMode.Open, FileAccess.ReadWrite);
				StreamReader strRead = new StreamReader(fStream);
				StreamWriter strWrite = new StreamWriter(fStream);
				
				NumGen(max, strRead, strWrite);
				
				strRead.Close();
				strWrite.Close();
			} else if(args.Length == 2) {
				if(args[1] != "-n") {
					Console.WriteLine("Second argument can only be -n");
					Environment.Exit(1);
				}
				LineCount lCount = new LineCount(args[0]);
				int max = MaxPadCount(lCount.GetLines());
				
				//creating the streams to read and write from given file
				FileStream fStream = new FileStream(args[0], FileMode.Open, FileAccess.ReadWrite);
				StreamReader strRead = new StreamReader(fStream);
				StreamWriter strWrite = new StreamWriter(fStream);
				
				NumRem(max, strRead, strWrite);
				
				strRead.Close();
				strWrite.Close();
			}
			
		}
		
		// indicate the amount of lines in the file
		public static int MaxPadCount(int lines) {
			return lines.ToString().Length;
		}
		
		//Generates linenumbers and outputs to the file
		public static void NumGen(int max, StreamReader strRead, StreamWriter strWrite) {
			string length;
			Padder pad = new Padder('0', max);
			int counter = 0;
			
			while((length = strRead.ReadLine()) != null) {
				strWrite.WriteLine(pad.Pad(++counter)+"  "+length);
				//Console.WriteLine(pad.Pad(++counter)+" "+length);
			}
			Console.WriteLine("Linenumbers added");
			strWrite.Flush();
		}
		
		//Removes linenumbers and outputs to the file
		public static void NumRem(int max, StreamReader strRead, StreamWriter strWrite) {
			//to be added in the future, first try to add linenumbers properly
		}
	}
	
	public class Padder {
		
		// makes of the linenumber a string
		public Padder(char cPad, int maxPad) {
			this.maxPad = maxPad;
			StringBuilder buffer = new StringBuilder(maxPad);
			
			for(int i=0; i < maxPad; i++)
				buffer.Append(cPad);
			
			pads = buffer.ToString();
		}
		
		public string Pad(int inp) {
			return Pad(inp.ToString());
		}
		
		public string Pad(string inp) {
			string strRet = inp;
			
			if((inp != null) && (inp.Length < maxPad)) {
				strRet = pads.Substring(0, (maxPad - inp.Length)) + inp;
			}
			
			return strRet;
		}
		
		private int maxPad;
		private string pads;
	}
	
	public class LineCount {
		// sets fname to filename indicated in Main method
		public LineCount(string fileName) {
			fName = fileName;
		}
		
		// counts the lines in the file
		public int GetLines() {
			StreamReader strRead = new StreamReader(new FileStream(fName, FileMode.Open));
			int iRet=0;
			
			while(strRead.ReadLine() != null)
				iRet++;
				
			strRead.Close();
			return iRet;
		}
		
		private String fName;
	}
}